package com.yeyks.commonReference.service.other.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.appcommon.dto.platform.AppCouponBasicDTO;
import com.yeyks.common.appcommon.dto.platform.AppCouponInfoDTO;
import com.yeyks.common.appcommon.param.platform.GetCouponDetailParam;
import com.yeyks.common.appcommon.param.platform.QueryCouponParam;
import com.yeyks.common.appcommon.result.CommonPage;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.constants.CouponConst;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.service.config.ConfigService;
import com.yeyks.commonReference.service.other.CouponInfoService;
import com.yeyks.other.dal.domain.CouponInfo;
import com.yeyks.other.dal.dao.CouponInfoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 券表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-06-25
 */
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {

    @Autowired
    private ConfigService configService;

//    @Value("#{${CustomProperties.coupon.SmallBlackFishSpree}}")
//    private Map<Integer, Integer> smallBlackFishSpreeAmountConfig;

    /**
     * 是否有推广所用的优惠券（true有，false没有）
     *
     * @return
     */
    @Override
    public Boolean recommendExist(Integer type) {
        if (CouponConst.recommend == type) {
            CouponInfo couponInfo = baseMapper.selectRecommendCoupon(type);
            return couponInfo != null;
        }
        if (CouponConst.bagGift == type) {
            List<CouponInfo> couponInfos = baseMapper.selectByType(type);
            if (couponInfos == null || couponInfos.size() == 0) {
                return false;
            }
            int count = 0;
            int totalCount = 0;
            Map<String, Integer> map = configService.getGiftCouponAmount();
            totalCount = map.get("total") == null ? 0 : map.get("total");
            for (CouponInfo couponInfo : couponInfos) {
                count += couponInfo.getRemainCount();
            }
            //大礼包券12张
            return count >= totalCount;
        }
        return false;
    }

    @Override
    public CommonResult<CommonPage<AppCouponBasicDTO>> selectPageByParam(QueryCouponParam param, AccountInfo accountInfo) {

        CommonPage<AppCouponBasicDTO> couponBasicDTOCommonPage = new CommonPage<>();

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<CouponInfo> couponInfos = baseMapper.selectList(null);
        if(!CollectionUtils.isEmpty(couponInfos)){
            CommonPage<CouponInfo> couponInfoCommonPage = CommonPage.restPage(param.getPageNum(), param.getLastId(), couponInfos);
            couponBasicDTOCommonPage = MyBeanUtils.convertCommonPageList(couponInfoCommonPage, AppCouponBasicDTO.class);
        }
        return CommonResult.success(couponBasicDTOCommonPage);
    }

    @Override
    public CommonResult<AppCouponInfoDTO> getCouponDetail(GetCouponDetailParam getCouponDetailParam, AccountInfo accountInfo) {

        CouponInfo couponInfo = baseMapper.selectOne(new QueryWrapper<CouponInfo>()
                .eq(CouponInfo.ID, getCouponDetailParam.getId()));
        if(null != couponInfo){

            AppCouponInfoDTO couponInfoDTO = MyBeanUtils.convert(couponInfo, AppCouponInfoDTO.class);
            return CommonResult.success(couponInfoDTO);
        }

        return CommonResult.failed("没有该优惠券的信息");
    }

    @Override
    public List<Map<String,Object>> statCouponUsed()
    {
        List<Map<String,Object>> list = baseMapper.statCouponUsed();
        if(list!=null&&list.size()>0)
        {
            for(Map map:list)
            {
                for(Object key:map.keySet()){//keySet获取map集合key的集合
                    Object value = map.get(key);//
                    if("recMoney".equalsIgnoreCase(key.toString())&&value==null)
                    {
                        break;
                    }
                    //System.out.println("key:"+key+" value:"+value);
                    if("recMoney".equalsIgnoreCase(key.toString())&&value!=null)
                    {
                        //计算退关优惠券数量信息
                        map.put("totalMoney", map.get("recMoney"));
                        map.put("totalCount", map.get("recCount"));
                    }
                }
            }
        }
        return list;
    }

    /**
     * 通过券id获取券信息
     *
     * @param couponId
     * @return
     */
    @Override
    public CouponInfo selectCouponInfo(Integer couponId) {
        return baseMapper.selectByPrimaryKey(couponId);
    }

    /**
     * 通过券id和用户id查询有效的券
     *
     * @param id
     * @param used     是否被使用
     * @param valid    是否有效
     * @param couponId 券id
     * @return
     */
    @Override
    public List<Map> selectByUser(Integer id, Boolean used, Boolean valid, Integer couponId) {
        return baseMapper.selectByUser(id, used, valid, couponId);
    }

    /**
     * 获取充值10元得到的50元的券
     *
     * @param payAmount
     * @return
     */
    @Override
    public CouponInfo selectCouponByPayAmount(Integer payAmount) {
        return baseMapper.selectCouponByPayAmount(payAmount);
    }

    /**
     * 通过优惠券id获取优惠券
     *
     * @param couponId
     * @return
     */
    @Override
    public CouponInfo selectByCouponId(Integer couponId) {
        return baseMapper.selectById(couponId);
    }

}
